//app.js
const host = require('./config').host

App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        this.globalData.code = res.code
      }
    })
    
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          try{
            this.globalData.token = wx.getStorageSync("token")

          } catch(e){}
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            withCredentials: true,
            // lang: "zh_TW",
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              console.log(res);
              this.globalData.userInfo = res.userInfo
              wx.request({
                url: host + 'api/tourist/weixin_login',
                data: {
                    encryptedData: res.encryptedData,
                    iv: res.iv,
                    code: this.globalData.code,
                    toekn: this.globalData.token
                },
                header: {
                    'content-type': 'application/json'
                },
                success: res => {
                    //从数据库获取用户信息
                    // that.queryUsreInfo();
                    console.log(res.data.data);
                      if(res.data.sta == 1){
                        this.globalData.userType = res.data.data.huiyuan[0].type
                        this.globalData.userId = res.data.data.huiyuan[0].id
                        this.globalData.uuid = res.data.data.uuid
                      }else{
                        wx.showModal({
                            title:'提示',
                            content:'登录出错',
                            showCancel:false,
                            confirmText:'返回授权',
                            success: res => {
                                if (res.confirm) {
                                    console.log('用户点击了“返回授权”')
                                } 
                            }
                        })
                      }
                    }
                });
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }else{

          try {
            const value = wx.getStorageSync("token")
            if (value) {
              // Do something with return value
              console.log(value)
              wx.request({
                url: host + 'api/tourist/dologin',
                data: {
                    token: value,
                },
                header: {
                    'content-type': 'application/json'
                },
                success: res => {
                    //从数据库获取用户信息
                    // that.queryUsreInfo();
                    // console.log(res.data.data);
                    // console.log(res.data.data.huiyuan[0].type);
                      if(res.data.sta == 1){
                        this.globalData.userType = res.data.data.huiyuan[0].type
                        this.globalData.userId = res.data.data.huiyuan[0].id
                        this.globalData.uuid = res.data.data.uuid
                      }else{
                        wx.showModal({
                            title:'提示',
                            content:'登录出错',
                            showCancel:false,
                            confirmText:'返回',
                            success: res => {
                                if (res.confirm) {
                                    console.log('用户点击了“返回授权”')
                                } 
                            }
                        })
                      }
                    }
                });
            }
          } catch (e) {
            // Do something when catch error
            // console.log(value)
          }
          
        }
      },
      fail: function(){
        // wx.navigate
      }
    })
  },
  checkSession: function(cb) {
    // console.log(this);
    var that = this
    wx.getSetting({
      success: res => {
          if (!res.authSetting['scope.userInfo']) {
            that.globalData.checkSession = false
            // that.globalData.userInfo = res.userInfo
            typeof cb == "function" && cb(that.globalData.checkSession)
            // console.log('d')
          }else{
            wx.checkSession({
              success: function(){
                that.globalData.checkSession = true
                typeof cb == "function" && cb(that.globalData.checkSession)
              },
              fail: function(){
                that.globalData.checkSession = false
                typeof cb == "function" && cb(that.globalData.checkSession)
              }
            })
          }
        }
      })
    // console.log(app.globalData.checkSession);
  },
  globalData: {
    userInfo: null,
    userType: null
  }

})
// const app = getApp()